package com.zc.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zc.blog.model.Article;
import com.zc.blog.model.Comment;
import com.zc.blog.service.*;
import com.zc.blog.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Objects;

@Controller
public class HomeController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ITagService tagService;

    @Value("${pageSize}")
    private int pageSize;

    @RequestMapping("/")
    public String home(Model model, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "query",required = false) String query) {
        List<Article> articleSliderList = articleService.findArticlesBySlider();//查询轮播图文章
        List<Article> articleList = articleService.listAll(page,pageSize,query); //获取文章
        articleList.removeIf(article -> Objects.equals(article.getImage(), "1.jpeg"));//迭代器删除
//        int total = articleList.size();
        for (Article article : articleList) {
            article.setCountComment(commentService.count(new QueryWrapper<Comment>().eq("articleId", article.getId())));
        }
        int total = articleService.getTotal(query);// 获取符合查询条件的文章总数

        String pageCode= PageUtil.genPagation("/", total, page, pageSize,query); // 生成分页链接

        model.addAttribute("articleSliderList", articleSliderList); //轮播图文章
        model.addAttribute("articleList", articleList);             //所有文章
        model.addAttribute("pageCode", pageCode);                   //前端分页代码
        model.addAttribute("query", query);                         //查询参数

        return "blog/index";
    }

    @RequestMapping("/contact")
    public String postContact() {
        return "blog/contact";
    }

    @RequestMapping("/page")
    public String page() {
        return "blog/page";
    }
    @RequestMapping("/category-lifestyle")
    public String lifestyle() {
        return "blog/articleCategory";
    }
    @RequestMapping("/category-travel")
    public String travel() {
        return "blog/category-travel";
    }
    @RequestMapping("/category-fashion")
    public String fashion() {
        return "blog/category-fashion";
    }
    @RequestMapping("/standard-post")
    public String standardPost() {
        return "blog/standard-post";
    }
    @RequestMapping("/music-post")
    public String musicPost() {
        return "blog/music-post";
    }
    @RequestMapping("/Video-post")
    public String VideoPost() {
        return "blog/Video-post";
    }
    @RequestMapping("/link-post")
    public String linkPost() {
        return "blog/link-post";
    }
    @RequestMapping("blog/404")
    public String four() {
        return "404";
    }
    @RequestMapping("blog/contact")
    public String contact() {
        return "blog/contact";
    }
    @RequestMapping("blog/gallery-post")
    public String galleryPost() {
        return "blog/articleRead";
    }

}
